﻿<%@ WebHandler Language="C#" Class="CommisionAdjust" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business;
using Chenxin.Lib.Business.BLL;

public class CommisionAdjust : AjaxHandlerBase
{
    public CommisionAdjust()
    {
        //导出
        htTypePermit.Add("export", Constants.Permit_CommisionAdjust_Export);
    }
    override public void RequestProcess(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";

            string strOp = context.Request["op"];
            string strRet = string.Empty;
            switch (strOp.ToLower())
            {
                //查询佣收调整信息
                case "getlist":
                    GetList(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

    private void GetList(HttpContext context)
    {
        //获取页面查询条件
        Guid city = (string.IsNullOrEmpty(context.Request["cityid"])) ? Guid.Empty : new Guid(context.Request["cityid"]);
        Guid company = (string.IsNullOrEmpty(context.Request["companyid"])) ? Guid.Empty : new Guid(context.Request["companyid"]);
        Guid businessdistrict = (string.IsNullOrEmpty(context.Request["areaid"])) ? Guid.Empty : new Guid(context.Request["areaid"]);
        Guid departs = (string.IsNullOrEmpty(context.Request["departid"])) ? Guid.Empty : new Guid(context.Request["departid"]);
        Guid employee = (string.IsNullOrEmpty(context.Request["userid"])) ? Guid.Empty : new Guid(context.Request["userid"]);

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(" 1=1 ");
        
        //判断级联菜单
        if (employee != Guid.Empty)
            sb.AppendFormat(" AND (a.ContractId in (select contractid from t_Contract_PointAllotInfo where EmployeeId='{0}')) ", employee);
        else if (departs != Guid.Empty)
            sb.AppendFormat(" AND (a.ContractId in (select contractid from t_Contract_PointAllotInfo where DepartId='{0}'))", departs);
        else if (businessdistrict != Guid.Empty)
        {
            Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo item = null;
            string strCachkey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, businessdistrict);
            Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
            item = cs.GetObject<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCachkey);
            if (!cs.Hit)
            {
                item = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(businessdistrict);
                if (item != null) cs.Set(strCachkey, item, DateTime.Now.AddDays(1));
            }

            if (item == null) context.Response.Write(string.Format("{{success:'0',msg:'{0}',data:''}}", Resources.Message.Global_Err_GetBusDis));

            sb.AppendFormat(" and (a.ContractId in (select contractid from t_Contract_PointAllotInfo where DepartId in (Select DepartId from v_Hr_DepartmentInfo where DepartSrc like '{0}%')))", item.DepartSrc);
        }
        else if (company != Guid.Empty)
            sb.AppendFormat(" and (a.ContractId in (select contractid from t_Contract_PointAllotInfo where DepartId in (Select DepartId from v_Hr_DepartmentInfo where CompanyId='{0}')))", company);
        else if (city != Guid.Empty)
            sb.AppendFormat(" and (a.ContractId in (select contractid from t_Contract_PointAllotInfo where DepartId in (Select DepartId from v_Hr_DepartmentInfo where CityId='{0}')))", city);
        else
            sb.AppendFormat(" and (a.ContractId in (select contractid from t_Contract_PointAllotInfo where DepartId in (Select DepartId from v_Hr_DepartmentInfo where CompanyId='{0}')))", CurrentUserInfo.CompanyId);

        if (!string.IsNullOrEmpty(context.Request["status"]))
        {
            switch (context.Request["status"])
            {
                case "50":
                    sb.Append(" and (b.AdjustType=10 OR b.AdjustType=20)");
                    break;
                case "60":
                    sb.Append(" and (b.AdjustType=30 OR b.AdjustType=40)");
                    break;
                default:
                    sb.AppendFormat(" and b.AdjustType='{0}'", context.Request["status"]);
                    break;
            }
        }
        string datetype = "b.Addtime";
        if (context.Request["datetype"] == "10")
            datetype = "a.ContractDate";
        else if (context.Request["datetype"] == "30")
            datetype = "a.ArchivementDate";
        else
            datetype = "b.Addtime";
        
        if (!string.IsNullOrEmpty(context.Request["starttime"]))
            sb.AppendFormat(" and {1}>='{0}'", context.Request["starttime"],datetype);
        if (!string.IsNullOrEmpty(context.Request["endtime"]))
            sb.AppendFormat(" and {1}<'{0}'", DateTime.Parse(context.Request["endtime"]).AddDays(1),datetype);
        if (!string.IsNullOrEmpty(context.Request["keyword"]))
            sb.AppendFormat(" and (b.ContractCode like '%{0}%' or a.HouseAddress like '%{0}%')", context.Request["status"]);
        
        
        try
        {
            //条件
            string strWhere = sb.ToString();
            //页码
            int strPageIndex = (string.IsNullOrEmpty(context.Request["pageindex"])) ? 1 : int.Parse(context.Request["pageindex"]) + 1;//页码
            //页显示行数
            int strPageSize = (string.IsNullOrEmpty(context.Request["pagesize"])) ? 30 : int.Parse(context.Request["pagesize"]);//页记录数
            //排序
            string OrderField = "b.Addtime DESC";
            if (!string.IsNullOrEmpty(context.Request["sort"]))
                OrderField = context.Request["sort"];
            
            //欲选择字段列表 
            string FieldList = "ContractCode,ContractDate,HouseAddress,AdjustReason,DiscountType,AdjustDescription,AdjustType,DiscountType,AdjustMoney,BeforeAdjust," +
                               "AfterAdjust,Addtime,AddDepart,AddEmployee,ArchivementDate";
            //子查询欲选择字段列表
            string TableFieldList = "b.ContractCode,ContractDate,HouseAddress,AdjustReason,AdjustDescription,AdjustMoney,BeforeAdjust,AfterAdjust,b.AddDepart,b.AddEmployee," +
                                    "(Case When AdjustType = 10 Then '上家应收' When AdjustType = 20 Then '下家应收' When AdjustType = 30 Then '上家咨询费' Else '下家咨询费' End) As AdjustType," +
                                    "(Case When DiscountType = 10 Then '事前折佣' Else '事后折佣' End) As DiscountType," +
                                    "REPLACE(CONVERT(Varchar(10),b.Addtime,111),'/','-') As Addtime,a.ArchivementDate";
            //所要查询的表名
            string TableName = "t_Contract_CommisionAdjustInfo as b inner join t_Contract_BaseInfo as a on b.ContractId=a.ContractId ";
            //根据sql执行查询
            DataSet ds = Chenxin.Lib.Business.DAL.t_Intention_BaseInfo.GetList(strPageIndex, strPageSize, OrderField, TableName, strWhere, FieldList, TableFieldList, true);

            context.Response.Write(string.Format("{{success:1,msg:'',data:{{count:{0},{1}}}}}", ds.Tables[1].Rows[0][0], Chenxin.Lib.Utility.JsonHelp.Convert2Json(ds.Tables[0])));
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }
}